import Vue from 'vue'
import Vuex from 'vuex'

//用于存储数据
const state = {
  token:'',
  menuList:[],
  isLoadRouters:false
}
//用于响应组件中的动作
const actions = {
  jia(context,value){
    context.commit('JIA',value)
  }
}
//用于操作数据
const mutations = {
  SET_TOKEN:(state,token)=>{
    state.token = token
    sessionStorage.setItem("token",token);
  },
  SET_MENULIST:(state,menuList)=>{
    state.menuList = menuList
    sessionStorage.setItem("menuList",JSON.stringify(menuList));
  },
  SET_ISLOADROUTERS:(state,isLoadRouters)=>{
    state.isLoadRouters = isLoadRouters
  }
}
//用于将state中的数据进行加工
const getters = {
  GET_TOKEN:state => {
    state.token = sessionStorage.getItem("token")
    return state.token
  },
  GET_MENULIST:state => {
    state.menuList = JSON.parse(sessionStorage.getItem("menuList")||'[]')
    return state.menuList
  },
  GET_ISLOADROUTERS:state=>{
    return state.isLoadRouters
  }
}
//引用vue插件
Vue.use(Vuex)
//创建store
export default new Vuex.Store({
  state,
  actions,
  mutations,
  getters
})
